<?php

declare(strict_types=1);

namespace app\admin\service\science;

use app\common\model\ScienceModel;
use app\common\model\ScienceCategoryModel;
use app\common\model\user\UserModel;
use Dengje\Pxy\BaseService;

class ScienceService extends BaseService
{
    protected $model = null;

    public function __construct()
    {
        $this->model = new ScienceModel();
    }

    public function list()
    {
        $where = [];
        $order = ['id' => 'desc'];
        $id = request()->get('id', '');
        $title = request()->get('title', '');
        $category_id = request()->get('category_id', '');
        $status = request()->get('status', '');
        $user_id = request()->get('user_id', '');
        if ($title) $where[] = ['title', 'like', '%' . $title . '%'];
        if ($id) $where[] = ['id', '=', $id];
        if ($category_id) $where[] = ['category_id', '=', $category_id];
        if ($status !== '') $where[] = ['status', '=', $status];
        if ($user_id) $where[] = ['user_id', '=', $user_id];
        $list = $this->model->where($where)->with(['category', 'user'])->order($order)->paginate(request()->get('page_size/d', 10));
        
        // 调整返回格式，确保前端能正确解析
        return [
            'list' => $list->items(),
            'total' => $list->total(),
            'page' => $list->currentPage(),
            'pageSize' => $list->listRows()
        ];
    }

    public function detail($id)
    {
        $detail = $this->model->where('id', $id)->with(['category', 'user'])->find();
        return $detail;
    }

    public function create($request)
    {
        $data = $this->preExcludeFields($request->param());
        $model = [
            'title' => $data['title'],
            'user_id' => $data['user_id'],
            'content' => $data['content'],
            'source' => $data['source'] ?? '',
            'category_id' => $data['category_id'] ?? 0,
            'cover_image' => $data['cover_image'] ?? '',
            'views' => $data['views'] ?? 0,
            'likes' => $data['likes'] ?? 0,
            'publish_time' => $data['publish_time'] ?? time(),
            'disclaimer' => $data['disclaimer'] ?? '',
            'status' => $data['status'] ?? 1,
            'create_time' => time(),
            'update_time' => time(),
        ];
        $id = $this->model->insertGetId($model);
        return $id;
    }

    public function update($id, $request)
    {
        $data = $this->preExcludeFields($request->param());
        $model = [
            'title' => $data['title'],
            'user_id' => $data['user_id'],
            'content' => $data['content'],
            'source' => $data['source'] ?? '',
            'category_id' => $data['category_id'] ?? 0,
            'cover_image' => $data['cover_image'] ?? '',
            'views' => $data['views'] ?? 0,
            'likes' => $data['likes'] ?? 0,
            'publish_time' => $data['publish_time'] ?? time(),
            'disclaimer' => $data['disclaimer'] ?? '',
            'status' => $data['status'] ?? 1,
            'update_time' => time(),
        ];
        $this->model->where('id', $id)->update($model);
        return $id;
    }

    public function delete($id)
    {
        // 检查是否有举报记录
        $reportCount = \app\common\model\ScienceReportModel::where('article_id', $id)->count();
        if ($reportCount > 0) {
            // 删除相关的举报记录
            \app\common\model\ScienceReportModel::where('article_id', $id)->delete();
        }
        $res =   $this->model->where('id', $id)->delete();

        return success('删除成功', $res);
    }

    public function select()
    {
        $list = $this->model->field('id, name')->select();
        return $list;
    }

    public function updateStatus($id, $status)
    {
        $this->model->where('id', $id)->update(['status' => $status, 'update_time' => time()]);
        return $id;
    }
}